package world.snowcrystal.exception;

import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import world.snowcrystal.dto.ApplicationMessage;

@ControllerAdvice
public class GlobalExceptionHandleController {



    @ExceptionHandler(SnowcrystalUserNotFoundException.class)
    public HttpEntity<ApplicationMessage> handleException(final SnowcrystalUserNotFoundException e) {

        return ResponseEntity.ok(ApplicationMessage.notFound(e.getMessage()));
    }

    @ExceptionHandler(BadCredentialsException.class)
    public HttpEntity<ApplicationMessage> handleException(final BadCredentialsException e) {
        return ResponseEntity.ok(ApplicationMessage.badCredentials(e.getMessage()));
    }
    @ExceptionHandler(SnowcrystalNoCredentialsProvidedException.class)
    public HttpEntity<ApplicationMessage> handleException(final SnowcrystalNoCredentialsProvidedException e) {
            return ResponseEntity.ok(ApplicationMessage.noCredentialsProvided(e.getMessage()));
    }


    @ExceptionHandler(Exception.class)
    public HttpEntity<ApplicationMessage> handleAny(final Exception e){
        return ResponseEntity.ok(ApplicationMessage.error(e.getMessage()));
    }

}
